package com.taotao.rop.service;

import com.rop.annotation.HttpAction;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import com.taotao.rop.database.domain.PlayGame;
import com.taotao.rop.database.domain.PlayGameRanking;
import com.taotao.rop.database.mapper.PlayGameMapper;
import com.taotao.rop.database.mapper.PlayGameRankingMapper;
import com.taotao.rop.response.BaseResponse;
import com.taotao.rop.util.DateTimeUtil;
import com.taotao.wechat.game.FindWordGame2;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@ServiceMethodBean(httpAction = HttpAction.POST, version = "1.0")
public class PlayGameRankingService {
    @Autowired
    private PlayGameRankingMapper playGameRankingMapper;
    @Autowired
    private PlayGameMapper playGameMapper;
    private final Object object = new Object();
    private final Object object2 = new Object();

    @ServiceMethod(method = "playGameRanking.list", needInSession = NeedInSessionType.NO)
    public Object list(PlayGameRanking request) {
        BaseResponse<List<PlayGameRanking>> response = new BaseResponse<List<PlayGameRanking>>();
        response.setSuccess(true);
        response.setData(playGameRankingMapper.getList(request));
        return response;
    }

    @ServiceMethod(method = "playGameRanking.insertOrUpdate", needInSession = NeedInSessionType.NO)
    public Object insertPlayGameRanking(PlayGameRanking request) {
        PlayGameRanking condition = new PlayGameRanking();
        condition.setGameName(request.getGameName());
        condition.setTel(request.getTel());

        List<PlayGameRanking> playGameRankingList = playGameRankingMapper.getList(condition);
        if (playGameRankingList == null || playGameRankingList.size() == 0) {
            synchronized (object) {
                playGameRankingList = playGameRankingMapper.getList(condition);
                if (playGameRankingList == null || playGameRankingList.size() == 0) {
                    playGameRankingMapper.insertOne(request);
                } else if (playGameRankingList.size() > 0) {
                    Integer score = Integer.parseInt(playGameRankingList.get(0).getResult());
                    Integer newScore = Integer.parseInt(request.getResult());
                    if (newScore > score) {
                        condition.setResult(request.getResult());
                        condition.setFinishTime(request.getFinishTime());
                        playGameRankingMapper.updateScore(condition);
                    }
                }
            }
        } else if (playGameRankingList.size() > 0) {
            Integer score = Integer.parseInt(playGameRankingList.get(0).getResult());
            Integer newScore = Integer.parseInt(request.getResult());
            if (newScore > score) {
                condition.setResult(request.getResult());
                condition.setFinishTime(request.getFinishTime());
                playGameRankingMapper.updateScore(condition);
            }
        }

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        return response;
    }

    @ServiceMethod(method = "playGameRanking.ranking", needInSession = NeedInSessionType.NO)
    public Object getRanking(PlayGameRanking request) {
        BaseResponse<List<PlayGameRanking>> response = new BaseResponse<List<PlayGameRanking>>();
        response.setSuccess(true);
        response.setData(playGameRankingMapper.getRanking(request));
        return response;
    }

    // 此定时器不再使用，用数据库存储过程和定时任务代替（存储过程名：find_word2_ranking_proc）
    // @Scheduled(cron = "0 1 0 * * ?")
    public synchronized void analyseRanking() {
        PlayGame condition = new PlayGame();
        condition.setGameName(new FindWordGame2().getNickName());
        Long endTime = DateTimeUtil.getDateStartMillis(new Date()) - 1;
        condition.setEndTime(endTime);
        condition.setStartTime(endTime + 1 - 24 * 3600 * 1000);
        List<PlayGame> playGameList = playGameMapper.getDailyPrizes(condition);
        //插入play game ranking 表中
        PlayGameRanking tempRanking = new PlayGameRanking();
        for (PlayGame playGame : playGameList) {
            tempRanking.setAppId(playGame.getAppId());
            tempRanking.setOpenId(playGame.getOpenId());
            tempRanking.setGameName(playGame.getGameName());
            tempRanking.setTel(playGame.getTel());
            tempRanking.setResult(playGame.getResult());
            tempRanking.setFinishTime(playGame.getFinishTime());
            int prize = playGame.getPrize();
            if (prize == 1) {
                tempRanking.setPrize(200);
            } else if (prize > 1 && prize <= 5) {
                tempRanking.setPrize(100);
            } else if (prize > 5 && prize <= 15) {
                tempRanking.setPrize(50);
            } else if (prize > 15 && prize <= 50) {
                tempRanking.setPrize(30);
            } else if (prize > 50 && prize <= 100) {
                tempRanking.setPrize(10);
            } else {
                tempRanking.setPrize(0);
            }

            if (tempRanking.getPrize() > 0) {
                PlayGameRanking insertOrUpdateCondition = new PlayGameRanking();
                insertOrUpdateCondition.setGameName(tempRanking.getGameName());
                insertOrUpdateCondition.setTel(tempRanking.getTel());
                List<PlayGameRanking> playGameRankingList = playGameRankingMapper.getList(insertOrUpdateCondition);
                if (playGameRankingList == null || playGameRankingList.size() == 0) {
                    synchronized (object2) {
                        playGameRankingList = playGameRankingMapper.getList(insertOrUpdateCondition);
                        if (playGameRankingList == null || playGameRankingList.size() == 0) {
                            playGameRankingMapper.insertOne(tempRanking);
                        } else {
                            //中奖手机号码已经存在，不再录入成绩
                        }
                    }
                } else {
                    //中奖手机号码已经存在，不再录入成绩
                }
            }
        }
    }
}
